<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/19
 * Time: 11:26
 */

namespace app\admin\controller;

use think\db;
use app\admin\model\Set as Set;
use app\admin\model\Ad as Ads;
use app\admin\model\Adtype as Adtype;

use app\index\controller\Common;

class Ad extends Common
{

    public function index()
    {
        $adlist = Ads::where('ad_status', 'in', '1')->select();
        $ad_type = Ads::ad_type();


        $this->assign('ad_type', $ad_type);
        $this->assign('adlist', $adlist);
        return view();
    }

    public function adconf($id = '')
    {
        if ($id) {
            $adconfFind = Ads::Where('id', 'in', $id)->find();
            $this->assign('adconfFind', $adconfFind);

        }
        if (request()->isAjax()) {
            $data = input('post.');
            if (isset($data['file'])) {
                unset($data['file']);
            }
            $data['ad_addtime'] = date('Y-m-d H:i:s', time());
            $data['ad_img_w'] = '100%';
            $data['ad_img_h'] = '100%';
            if ($id) {
                $rows = Ads::update($data);
                $this->success('修改成功', url('Ad/index'));
            } else {
                $rows = Ads::create($data);
                if ($rows) {
                    $this->success('添加成功', url('Ad/index'));
                } else {
                    $this->success('添加失败');
                }
            }

        }

        $ad_type = Ads::ad_type();
        $this->assign('ad_type', $ad_type);
        return view();
    }

    public function addel()
    {

        if (request()->isAjax()) {
            $adId = input('post.id');
            $row = Ads::DelAdLst($adId);
            if ($row) {
                if (Ads::where('id', 'in', $adId)->delete()) {
                    $this->success('删除成功', url('Ad/index'));
                } else {
                    $this->error('删除失败');
                }
            }
            return $this->error('系统预留广告位无法删除');

        }
    }

    public function adtype()
    {
//        dump(input(''));die;
        if (request()->isAjax()) {
            $validate = validate('ad');
            if (!$validate->scene('add')->check(input('post.'))) {
                $this->error($validate->getError());
            }
            if (Ads::add_type(input('post.'))) {
                $this->success('添加成功', url('Ad/index'));
            } else {
                $this->error('添加失败');

            }
        }
    }

}